class Appart < ActiveRecord::Base
  belongs_to :ville, :class_name => 'Villes', :foreign_key => 'ville_id'
  
  def self.MoyMetreCarre
    find_by_sql("select *, ROUND(avg(prix/taille),2) as valeur, COUNT(PRIX) as nbr 
        from apparts group by ville_id order by valeur");
  end
  
  def self.MaxByMetre(metre)
    find_by_sql(["select MAX(PRIX) as val,id,ville_id
                from apparts where taille = ?
                group by taille", metre])
  end
  
  def self.MinByMetre(metre)
    find_by_sql(["select MIN(PRIX) as val,id,ville_id
                from apparts where taille = ?
                group by taille", metre])
  end
 
 def self.AvgByMetre(metre)
    find_by_sql(["select ROUND(AVG(PRIX),2) as val,id,ville_id
                from apparts where taille = ? group by taille", metre])
 end

 def self.CountByMetre(metre)
   find_by_sql(["select COUNT(id) as nbr from apparts
                where taille = ?", metre])
 end
 
end
